package Arrays;

public class _695_MaxAreaofIsland {
    int [][] grid;
    boolean [][] seen;
    public int maxAreaOfIsland(int[][] grid) {
        this.grid = grid;
        this.seen = new boolean[grid.length][grid[0].length];
        int max = Integer.MIN_VALUE;
        for(int i=0;i<grid.length;i++) {
            for(int j=0;j<grid[0].length;j++) {
                max = Math.max(max,area(i,j));
            }
        }
        return max;
    }

    public int area(int hor,int ver) {
        if (hor < 0 || hor >= grid.length || ver >= grid[0].length || ver < 0 || seen[hor][ver] || grid[hor][ver] == 0) {
            return 0;
        }
        seen[hor][ver] = true;
        return 1+(area(hor+1,ver)+area(hor-1,ver)+area(hor,ver+1)+area(hor,ver-1));
    }
}
